﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace ĐỒ_ÁN_QLDA
{
    public partial class frmGiaovien : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommandBuilder cmb;
        
        String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;

        public frmGiaovien()
        {
            
            conn = new SqlConnection(conStr);
            conn.ConnectionString = conStr;
            InitializeComponent();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            txtMagv.Text = "";
            txtTengv.Text = "";
            txtDiachi.Text = "";
            txtSdt.Text = "";
            txtChuyenmon.Text = "";
            txtMagv.Focus();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.CurrentRow.Index;
            ds.Tables[0].Rows[i].Delete();
            da.Update(ds, "Giaovien");
            MessageBox.Show("Xóa thành công");
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            string magv = txtMagv.Text;
            string tengv = txtTengv.Text;

            string diachi = txtDiachi.Text;
            string dt = txtSdt.Text;
            string chuyenmon = txtChuyenmon.Text;

            //int mag = Int32.Parse(magv);

            conn.Open();

            cmd = new SqlCommand("Update Giaovien set MaGiaoVien='" + magv + "', TenGiaoVien = '" + tengv + "', DiaChi = '" + diachi + "', DienThoai = '" + dt + "', MaMonHoc = '" + chuyenmon + "' where MaGiaoVien=" + magv + "", conn);
            da = new SqlDataAdapter(cmd);
            int i = cmd.ExecuteNonQuery();
            ds = new DataSet();
            da.Fill(ds, "Giaovien");
            cmb = new SqlCommandBuilder(da);

            conn.Close();
                
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMagv.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtTengv.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            
            txtDiachi.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();

            txtSdt.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            txtChuyenmon.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            txtMagv.Text = "";
            txtTengv.Text = "";
            txtDiachi.Text = "";
            txtSdt.Text = "";
            txtChuyenmon.Text = "";
            txtMagv.Focus();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.CurrentRow.Index;
            ds.Tables[0].Rows[i].Delete();
            da.Update(ds, "Giaovien");
            MessageBox.Show("Xóa thành công");
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            frmMenu objfrmMenu = new frmMenu();
            this.Dispose();
            objfrmMenu.ShowDialog();
        }

        private void btThoat_Click_1(object sender, EventArgs e)
        {
            frmMenu objfrmMenu = new frmMenu();
            this.Dispose();
            objfrmMenu.ShowDialog();
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMagv.Text == "" || txtTengv.Text == "" || txtDiachi.Text == "" || txtSdt.Text == "" || txtChuyenmon.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền đầy đủ thông tin", "Có lỗi xảy ra!");
                    txtMagv.Focus();
                    return;
                }
                else
                {
                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM Giaovien", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "Giaovien");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                    DataTable dt = ds.Tables[0];
                    DataRow newRow = dt.NewRow();
                    newRow["MaGiaoVien"] = txtMagv.Text;
                    newRow["TenGiaoVien"] = txtTengv.Text;

                    newRow["DiaChi"] = txtDiachi.Text;
                    newRow["DienThoai"] = txtSdt.Text;
                    newRow["ChuyenMon"] = txtChuyenmon.Text;


                    dt.Rows.Add(newRow);
                    da.Update(ds, "Giaovien");
                    MessageBox.Show("Thêm thành công");
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
                txtMagv.Focus();

            } 
        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            txtMagv.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtTengv.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            
            txtDiachi.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();

            txtSdt.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            txtChuyenmon.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
        }

        private void frmGiaovien_Load(object sender, EventArgs e)
        {
            ds = new DataSet();
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd = new SqlCommand("Select * from Giaovien", conn);
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "Giaovien");
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }
        }

      
    }
}
